/*
 * Copyright (c) 2016- 2020 qolome.com Inc. All Rights Reserved.
 * ProjectName: qolome-knowledge
 * FileName: TicketFactory.java
 * Author: jiangfeng@qolome.com
 * Date: 2020/3/9 下午4:49
 * LastModified: 2020/03/09 16:49:09
 */

package com.qolome.knowledge.design.pattern.flyweight.ticket;

import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Qolome
 * @version V1.0
 * @Description 车票工厂
 * @date 2020年 03月09日 16:49:17
 */
@Slf4j
public class TicketFactory {

    private static Map<String,ITicket> pool = new HashMap<>();

    public static  ITicket queryTicket(String from, String to){
        String key = from+"->"+to;
        if(pool.containsKey(key)){
            log.info("使用缓存:{}",key);
            return pool.get(key);
        }

        log.info("首次查询,创建对象:{}",key);
        ITicket ticket = new TrainTicket(from,to);
        pool.put(key,ticket);
        return ticket;

    }

}
